﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using PMIS.BLL;
using PMIS.BLL.Department;
using PMIS.BLL.Post;
using PMIS.UI.Department;

namespace PMIS.UI.Post
{
	/// <summary>
	/// 岗位信息维护
	/// </summary>
	public partial class 岗位信息维护 : BaseForm
	{
		#region 私有变量
		/// <summary>
		/// 当前操作的记录
		/// </summary>
		private 岗位 CurrentRecord = null;
		#endregion

		#region 单例
		/// <summary>
		/// 本类的实例
		/// </summary>
		private static 岗位信息维护 _form = null;
		/// <summary>
		/// 用于本类实现单例的Locker
		/// </summary>
		private static object locker = new object();
		/// <summary>
		/// 构造函数
		/// </summary>
		private 岗位信息维护()
		{
			InitializeComponent();
		}
		/// <summary>
		/// 获取本窗体的实例
		/// </summary>
		/// <returns></returns>
		public static 岗位信息维护 GetInstance(string ID)
		{
			if (_form == null || _form.IsDisposed)
			{
				lock (locker)
				{
					if (_form == null || _form.IsDisposed)
						_form = new 岗位信息维护();
				}
			}

			if (ID == "new")
			{
				_form.CurrentRecord = new 岗位();
			}
			else
			{
				_form.CurrentRecord = Singleton<BL_岗位>.GetInstance().GetEntity(ID);
			}

			return _form;
		}
		#endregion

		#region 窗体事件
		/// <summary>
		/// 窗体载入事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void 岗位信息维护_Load(object sender, System.EventArgs e)
		{
			部门班组选择.SelectionChanged += new EventHandler<NotifyEventArgs<部门>>(部门班组选择_SelectionChanged);
			//	更新界面显示
			UpdateFormView();
		}
		/// <summary>
		/// 窗体关闭事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void 岗位信息维护_FormClosed(object sender, FormClosedEventArgs e)
		{
			部门班组选择.SelectionChanged -= new EventHandler<NotifyEventArgs<部门>>(部门班组选择_SelectionChanged);
		}
		#endregion

		#region 界面显示
		/// <summary>
		/// 更新界面显示
		/// </summary>
		private void UpdateFormView()
		{
			try
			{
				#region 分类等级显示
				//	获取信息
				List<string> list = BL_岗位.岗位分类;
				List<岗位_等级> post = string.IsNullOrEmpty(CurrentRecord.ID) ? new List<岗位_等级>() : Singleton<BL_岗位_等级>.GetInstance().GetDetails(CurrentRecord.ID);
				List<岗位_分类_等级> level = Singleton<BL_岗位_分类_等级>.GetInstance().GetAll();
				if (list == null || post == null || level == null)
					throw new Exception("获取岗位分类等级信息时发生错误!");

				int index = 0;

				List<GroupBox> gbs = new List<GroupBox>();
				foreach (Control ct in panel1.Controls)
				{
					if (ct is GroupBox)
					{
						gbs.Add(ct as GroupBox);
					}
				}
				//	按照ID排序
				gbs.Sort(delegate(GroupBox x, GroupBox y) { return x.Name.CompareTo(y.Name); });

				foreach (Control ct in gbs)
				{
					GroupBox gb = ct as GroupBox;
					gb.Text = list[index];

					if (gb.Controls[0] is CheckBox)
					{
						CheckBox cb = gb.Controls[0] as CheckBox;
						cb.CheckedChanged -= new EventHandler(cb_CheckedChanged);
						cb.CheckedChanged += new EventHandler(cb_CheckedChanged);
					}

					if (gb.Controls[1] is CheckedListBox)
					{
						CheckedListBox clb = gb.Controls[1] as CheckedListBox;
						clb.CheckOnClick = true;
						clb.BeginUpdate();
						clb.Items.Clear();
						List<岗位_分类_等级> items = level.FindAll(t => t.分类 == list[index]);
						if (items == null)
							throw new Exception("获取分类等级信息时发生错误!");

						for (int i = 0; i < items.Count; i++)
						{
							clb.Items.Add(items[i]);
							clb.SetItemChecked(i, post.Exists(t => t.分类 == list[index] && t.等级ID == items[i].ID));
						}
						clb.EndUpdate();
					}

					index++;
				}
				#endregion

				#region 岗位基本信息显示
				//	岗位代码
				tbCode.Text = CurrentRecord.岗位代码;
				//	岗位名称
				tbName.Text = CurrentRecord.岗位名称;
				//	部门
				部门 department = Singleton<BL_部门>.GetInstance().GetEntity(CurrentRecord.部门班组ID);
				tbDepartment.Text = department == null ? string.Empty : department.路径;
				tbDepartment.Tag = CurrentRecord.部门班组ID;
				//	是否一线岗位
				cbFirst.Checked = CurrentRecord.是否一线岗位 == "是";
				//	备注
				tbRemark.Text = CurrentRecord.备注;
				#endregion
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		#endregion

		#region 触发事件
		/// <summary>
		/// 选择部门班组
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tbDepartment_Click(object sender, EventArgs e)
		{
			try
			{
				部门班组选择 form = 部门班组选择.GetInstance();
				if (form == null)
					throw new Exception("无法获取部门班组选择窗体");

				form.ShowDialog(this);
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 部门班组选择结果
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void 部门班组选择_SelectionChanged(object sender, NotifyEventArgs<部门> e)
		{
			try
			{
				tbDepartment.Text = e.value.路径;
				tbDepartment.Tag = e.value.ID;
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 全选
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void cb_CheckedChanged(object sender, EventArgs e)
		{
			try
			{
				CheckBox cb = sender as CheckBox;
				CheckedListBox clb = cb.Parent.Controls[1] as CheckedListBox;
				for (int index = 0; index < clb.Items.Count; index++)
				{
					clb.SetItemChecked(index, cb.Checked);
				}
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 检查用户输入
		/// </summary>
		/// <returns></returns>
		private bool CheckInput()
		{
			if (string.IsNullOrEmpty(tbCode.Text.Trim()))
			{
				ShowErrorMessage("请输入岗位代码!");
				tbCode.Focus();
				return false;
			}

			if (string.IsNullOrEmpty(tbName.Text.Trim()))
			{
				ShowErrorMessage("请输入岗位名称!");
				tbName.Focus();
				return false;
			}

			if (string.IsNullOrEmpty(tbDepartment.Text.Trim()))
			{
				ShowErrorMessage("请输入部门!");
				tbDepartment.Focus();
				return false;
			}

			return true;
		}
		/// <summary>
		/// 保存
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsbtSave_Click(object sender, EventArgs e)
		{
			if (!CheckInput())
				return;

			try
			{
				CurrentRecord.岗位代码 = tbCode.Text.Trim();
				CurrentRecord.岗位名称 = tbName.Text.Trim();
				CurrentRecord.部门班组ID = tbDepartment.Tag as string;
				CurrentRecord.是否一线岗位 = cbFirst.Checked ? "是" : "否";
				CurrentRecord.备注 = tbRemark.Text.Trim();

				List<岗位_等级> list = new List<岗位_等级>();
				foreach (Control ct in panel1.Controls)
				{
					if (ct is GroupBox)
					{
						CheckedListBox clb = (ct as GroupBox).Controls[1] as CheckedListBox;
						foreach (int index in clb.CheckedIndices)
						{
							岗位_分类_等级 i = clb.Items[index] as 岗位_分类_等级;
							岗位_等级 item = new 岗位_等级();
							item.ID = Guid.NewGuid().ToString("D");
							item.分类 = i.分类;
							item.等级ID = i.ID;

							list.Add(item);
						}
					}
				}

				//	保存
				if (Singleton<BL_岗位>.GetInstance().Save(CurrentRecord, list))
				{
					ShowSuccessMessage("岗位保存成功!");
					DialogResult = DialogResult.OK;
					this.Close();
				}
				else
				{
					ShowErrorMessage("保存岗位时发生错误,请检查岗位代码是否重复!");
				}
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 退出
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsbtExit_Click(object sender, EventArgs e)
		{
			this.Close();
		}
		#endregion
	}
}
